<!--********************************************************************
* Copyright© 2000 - 2018 SuperMap Software Co.Ltd. All rights reserved.
*********************************************************************-->
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <title data-i18n="resources.title_kernelDensityJobService"></title>
    <script type="text/javascript" src="../../dist/mapboxgl/include-mapboxgl.js"></script>
    <style>
        body, #map {
            position: absolute;
            width: 100%;
            height: 100%
        }

        .panel-default {
            width: 300px;
            float: right;
            margin: 15px 15px 0 0;
            position: relative;
        }

        .input-group {
            margin-bottom: 15px;
        }
    </style>
</head>

<body>
    <div id="map"></div>
    <div class='panel panel-default'>
        <div class='panel-heading'>
            <h3 class='panel-title' data-i18n="resources.text_kernelDensityJobService"></h3>
        </div>
        <div class='panel-body'>
            <div class='input-group'>
                <span class='input-group-addon'><span data-i18n="resources.text_inputData"></span>
                    <span data-i18n="[title]resources.text_requiredField" style='color: red;'>*</span> 　</span>
                <input id='datasetName' type='text' class='form-control' value='samples_newyork_taxi_2013-01_14k' /></div>
            <div class='input-group'>
                <span class='input-group-addon'><span data-i18n="resources.text_analysisMethod"></span>
                    <span data-i18n="[title]resources.text_requiredField" style='color: red;'>*</span> 　</span>
                <div>
                    <select class='form-control' id='method' name='clientType'>
                        <option value='0' selected='selected' data-i18n="resources.text_simpleDenAnalyst"></option>
                        <option value='1' data-i18n="resources.text_input_value_densityAnalyst"></option>
                    </select>
                </div>
            </div>
            <div class='input-group'>
                <span class='input-group-addon'><span data-i18n="resources.text_meshSurfaceType"></span>
                    <span data-i18n="[title]resources.text_requiredField" style='color: red;'>*</span></span>
                <select class='form-control' id='meshType' name='clientType'>
                    <option value='0' selected='selected' data-i18n="resources.text_4grid"></option>
                    <option value='1' data-i18n="resources.text_6grid"></option>
                </select></div>
            <div class='input-group'>
                <span class='input-group-addon' data-i18n="resources.text_weightField"></span>
                <input id='fields' type='text' class='form-control' value='col7,col8' /></div>
            <div class='input-group'>
                <span class='input-group-addon' data-i18n="resources.text_analysisRange"></span>
                <input id='query' type='text' class='form-control' value='-74.150, 40.550, -73.750, 40.950' /></div>
            <div class='input-group'>
                <span class='input-group-addon'><span data-i18n="resources.text_gridSize"></span>
                    <span data-i18n="[title]resources.text_requiredField" style='color: red;'>*</span> 　</span>
                <input id='resolution' type='text' class='form-control' value='80' /></div>
            <div class='input-group'>
                <span class='input-group-addon' data-i18n="resources.text_gridSizeUnit"></span>
                <select class='form-control' id='meshSizeUnit' name='clientType'>
                    <option value='Meter' selected='selected'>Meter</option>
                    <option value='Kilometer'>Kilometer</option>
                    <option value='Yard'>Yard</option>
                    <option value='Foot'>Foot</option>
                    <option value='Mile'>Mile</option>
                </select></div>
            <div class='input-group'>
                <span class='input-group-addon'><span data-i18n="resources.text_searchRadius"></span>
                    <span data-i18n="[title]resources.text_requiredField" style='color: red;'>*</span> 　</span>
                <input id='radius' type='text' class='form-control' value='300' /></div>
            <div class='input-group'>
                <span class='input-group-addon' data-i18n="resources.text_searchRadiusUnit"></span>
                <select class='form-control' id='radiusUnit' name='clientType'>
                    <option value='Meter' selected='selected'>Meter</option>
                    <option value='Kilometer'>Kilometer</option>
                    <option value='Yard'>Yard</option>
                    <option value='Foot'>Foot</option>
                    <option value='Mile'>Mile</option>
                </select></div>
            <div class='input-group'>
                <span class='input-group-addon' data-i18n="resources.text_areaUnit"></span>
                <select class='form-control' id='areaUnit' name='clientType'>
                    <option value='SquareMeter'>SquareMeter</option>
                    <option value='Hectare'>Hectare</option>
                    <option value='Are'>Are</option>
                    <option value='Acre'>Acre</option>
                    <option value='SquareFoot'>SquareFoot</option>
                    <option value='SquareYard'>SquareYard</option>
                    <option value='SquareMile' selected='selected'>SquareMile</option>
                </select></div>
            <div align='right'>
                <input type='button' id='btn' class='btn btn-primary' data-i18n="[value]resources.text_analyst" />
            </div>
        </div>
    </div>
    <script type="text/javascript" include="jquery,bootstrap,widgets" src="../js/include-web.js"></script>
    <script type="text/javascript">
        var host = window.isLocal ? window.server : "http://114.115.213.31:8090";

        var attribution = "<a href='https://www.mapbox.com/about/maps/' target='_blank'>© Mapbox </a>" +
            " with <span>© <a href='http://iclient.supermap.io' target='_blank'>SuperMap iClient</a> | </span>" +
            "Map Data <span>© <a href='http://support.supermap.com.cn/product/iServer.aspx' target='_blank'>SuperMap iServer</a></span> ",


            processingUrl = host + "/iserver/services/distributedanalyst/rest/v1/jobs";

        var map = new mapboxgl.Map({
            container: 'map', // container id
            style: {
                "version": 8,
                "sources": {
                    "raster-tiles": {
                        "attribution": attribution,
                        "type": "raster",
                        "tiles": [host +
                            '/iserver/services/map-china400/rest/maps/China/zxyTileImage.png?z={z}&x={x}&y={y}'
                        ],
                        "tileSize": 256
                    }
                },
                "layers": [{
                    "id": "simple-tiles",
                    "type": "raster",
                    "source": "raster-tiles",
                    "maxzoom": 18,
                    "minzoom": 2,
                }]
            },
            center: [-73.9, 40.74],
            zoom: 11
        });
        map.addControl(new mapboxgl.NavigationControl(), 'top-left');
        map.addControl(new mapboxgl.supermap.LogoControl(), 'bottom-right');

        SuperMap.SecurityManager.registerToken(processingUrl, window.exampleToken);

        function getQuery() {
            if ($('#query').val() === "") {
                return "";
            }
            var query = [];
            $('#query').val().split(',').map(function (el) {
                query.push(parseFloat(el));
            });
            var sw = new mapboxgl.LngLat(query[0], query[1]);
            var ne = new mapboxgl.LngLat(query[2], query[3]);

            return new mapboxgl.LngLatBounds(sw, ne);
        }

        $('#btn').on('click', function () {
            if ($('#msg_container')[0]) {
                $('#msg_container').remove();
            }
            widgets.loader.showLoader();
            if (map && map.getLayer("kernerlLayer")) {
                map.removeLayer("kernerlLayer")
                map.removeSource("kernerlSource");
            }
            var kernelDensityJobParameter = new SuperMap.KernelDensityJobParameter({
                datasetName: $('#datasetName').val(),
                resolution: $('#resolution').val(),
                method: $('#method option:selected').attr('value'),
                meshType: $('#meshType option:selected').attr('value'),
                fields: $('#fields').val(),
                query: getQuery(),
                radius: $('#radius').val(),
                meshSizeUnit: $('#meshSizeUnit option:selected').attr('value'),
                radiusUnit: $('#radiusUnit option:selected').attr('value'),
                areaUnit: $('#areaUnit option:selected').attr('value'),
            });

            new mapboxgl.supermap.ProcessingService(processingUrl, {
                withCredentials: window.isLocal
            }).addKernelDensityJob(kernelDensityJobParameter, function (serviceResult) {
                if (serviceResult.error) {
                    widgets.loader.removeLoader();
                    var errorMsg = serviceResult.error.errorMsg || "code: " + serviceResult.error.code;
                    widgets.alert.showAlert(resources.msg_createFailed + "<br>" + errorMsg, false);
                    return;
                }
                serviceResult.result.setting.serviceInfo.targetServiceInfos.map(function (info) {
                    if (info.serviceType === 'RESTMAP') {
                        SuperMap.FetchRequest.get(info.serviceAddress + '/maps').then(function (
                            response) {
                            return response.json();
                        }).then(function (result) {
                            var mapUrl = result[0].path +
                                "/zxyTileImage.png?z={z}&x={x}&y={y}&width=256&height=256&transparent=true";
                            map.addSource("kernerlSource", {
                                "type": "raster",
                                "tiles": [mapUrl],
                                "tileSize": 256
                            });
                            map.addLayer({
                                "id": "kernerlLayer",
                                "type": "raster",
                                "source": "kernerlSource",
                                "minzoom": 0,
                                "maxzoom": 22
                            });
                            widgets.loader.removeLoader();
                        });
                    }
                });
            });

        });
    </script>
</body>

</html>